package com.etc.selenium;

import com.etc.TestMain1;
import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestMain1.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ActTest1 {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Autowired
    private ProcessEngine processEngine;

    // 开启进程
    @Test
    public void test1() {
// 签收
        Map<String, Object> varible = new HashMap();
        varible.put("username", "TOM");
//        varible
//        varible
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("myProcess_1", varible);
        System.out.println(instance.getId());

//        instance.getStartUserId();

    }

    /**查询当前人的个人任务*/
    @Test
    public void findMyPersonalTask(){
        String assignee = "TOM";
        List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
                .createTaskQuery()//创建任务查询对象
                /**查询条件（where部分）*/
                .taskAssignee(assignee)//指定个人任务查询，指定办理人
//						.taskCandidateUser(candidateUser)//组任务的办理人查询
//						.processDefinitionId(processDefinitionId)//使用流程定义ID查询
//						.processInstanceId(processInstanceId)//使用流程实例ID查询
//						.executionId(executionId)//使用执行对象ID查询
                /**排序*/
                .orderByTaskCreateTime().asc()//使用创建时间的升序排列
                /**返回结果集*/
//						.singleResult()//返回惟一结果集
//						.count()//返回结果集的数量
//						.listPage(firstResult, maxResults);//分页查询
                .list();//返回列表
        if(list!=null && list.size()>0){
            for(Task task:list){
                System.out.println("任务ID:"+task.getId());
                System.out.println("任务名称:"+task.getName());
                System.out.println("任务的创建时间:"+task.getCreateTime());
                System.out.println("任务的办理人:"+task.getAssignee());
                System.out.println("流程实例ID："+task.getProcessInstanceId());
                System.out.println("执行对象ID:"+task.getExecutionId());
                System.out.println("流程定义ID:"+task.getProcessDefinitionId());
                System.out.println("########################################################");
            }
        }
    }

    /**查询流程状态（判断流程正在执行，还是结束）*/
    @Test
    public void isProcessEnd(){
        String processInstanceId = "50005"; // 50005
        ProcessInstance pi = processEngine.getRuntimeService()//表示正在执行的流程实例和执行对象
                .createProcessInstanceQuery()//创建流程实例查询
                .processInstanceId(processInstanceId)//使用流程实例ID查询
                .singleResult();
        if(pi==null){
            System.out.println("流程已经结束");
        }
        else{
            System.out.println("流程没有结束");
        }
    }

    /**查询流程状态（判断流程正在执行，还是结束）*/
    @Test
    public void tomshenqing(){
        // 无 owner的可以签收任务
//        taskService.claim("37505", "TOM");

        // 50011
        Task task = processEngine.getTaskService().createTaskQuery().taskId("50011").singleResult();
        Map m1 = new HashMap();
        m1.put("fromdate", new Date());
        m1.put("reason", "肚子痛");
        //提交表单（带参数）
        formService.submitTaskFormData(task.getId(), m1);
//        taskService.complete(task.getId(), m1);
    }

    @Autowired
    private HistoryService historyService;
    @Autowired
    private FormService formService;

    // 任务历史记录
    @Test
    public void tomyiban() {
        String assignee = "TOM";
        List<HistoricTaskInstance> taskList  = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .finished()
                .list();


        for (HistoricTaskInstance task : taskList) {
            System.out.println("任务ID:"+task.getId());
            System.out.println("任务名称:"+task.getName());
            System.out.println("任务的创建时间:"+task.getCreateTime());
            System.out.println("任务的办理人:"+task.getAssignee());
            System.out.println("流程实例ID："+task.getProcessInstanceId());
            System.out.println("执行对象ID:"+task.getExecutionId());
            System.out.println("流程定义ID:"+task.getProcessDefinitionId());
            System.out.println("########################################################");
        }
    }


    /** 审核 */
    @Test
    public void jerryshenhe(){
        // 无 owner的可以签收任务
//        taskService.claim("37505", "TOM");
        String assignee = "jerry";
//        taskService.claim(, assignee);
        // 50011
        Task task = processEngine.getTaskService().createTaskQuery().taskAssignee("jerry").singleResult();
        System.out.println("任务ID:"+task.getId());
        System.out.println("任务名称:"+task.getName());
        System.out.println("任务的创建时间:"+task.getCreateTime());
        System.out.println("任务的办理人:"+task.getAssignee());
        System.out.println("流程实例ID："+task.getProcessInstanceId());
        System.out.println("执行对象ID:"+task.getExecutionId());
        System.out.println("流程定义ID:"+task.getProcessDefinitionId());
        System.out.println("########################################################");
        TaskFormData formdata = formService.getTaskFormData(task.getId());
        List<FormProperty> list = formdata.getFormProperties();
        for (FormProperty formProperty : list) {
            System.out.println(formProperty.getName() + "@" + formProperty.getValue());

        }

//        taskService.complete(task.getId());
    }
}
